package com.hh.controller;

import com.hh.common.BaseResponse;
import com.hh.common.ErrorCode;
import com.hh.common.ResultUtils;
import com.hh.entity.domain.Comments;
import com.hh.entity.dto.comment.CommentsAddRequest;
import com.hh.entity.dto.comment.CommentsQueryRequest;
import com.hh.entity.vo.CommentTreeVO;
import com.hh.exception.ThrowUtils;
import com.hh.service.CommentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * com.hh.controller
 *
 * @author hanhui
 * @version 0.0.1
 * @date 2025/7/20
 **/
@RestController
@Slf4j
@RequestMapping("/comments")
public class CommentsController {

    @Resource
    private CommentsService commentsService;

    /**
     * 添加评论
     *
     * @param commentsAddRequest 添加评论的请求对象，包含评论内容等信息
     * @param request HTTP请求对象，用于获取用户会话信息
     * @return BaseResponse<Boolean> 返回操作结果，成功返回true，失败返回错误信息
     */
    @GetMapping("/add")
    public BaseResponse<Boolean> addComments(CommentsAddRequest commentsAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(commentsAddRequest == null, ErrorCode.PARAMS_NULL);
        boolean result = commentsService.addComments(commentsAddRequest, request);
        if (!result) {
            return new BaseResponse<>(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    /**
     * 分页获取评论列表（树形结构）
     *
     * @param commentsQueryRequest 评论查询请求对象，包含分页参数和查询条件
     * @param request HTTP请求对象，用于获取用户会话信息
     * @return BaseResponse<List<CommentTreeVO>> 返回评论树形结构列表
     */
    @GetMapping("/list/vo/page")
    public BaseResponse<List<CommentTreeVO>> listCommentsVOByPage(CommentsQueryRequest commentsQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(commentsQueryRequest == null, ErrorCode.PARAMS_NULL);
        List<Comments> list = commentsService.list(commentsService.getQueryWrapper(commentsQueryRequest));
        List<CommentTreeVO> treeVOS = commentsService.getCommentsVOTree(list);
        return ResultUtils.success(treeVOS);

    }
}

